/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.common.enums.CampusinfoOrderStat;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoCountry;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoRegion;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoCountryService;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoRegionService;
import com.lczyfz.campusinfo.modules.campusinfopay.entity.CampusinfoPay;
import com.lczyfz.campusinfo.modules.campusinfopay.service.CampusinfoPayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoAbroad;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoAbroadService;

import java.util.List;

/**
 * 出国信息管理Controller
 *
 * @author yt
 * @version 2017-10-20
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoeducation/campusinfoAbroad")
public class CampusinfoAbroadController extends BaseController {
    @Autowired
    private CampusinfoPayService campusinfoPayService;
    @Autowired
    private CampusinfoAbroadService campusinfoAbroadService;

    @Autowired
    private CampusinfoRegionService campusinfoRegionService;

    @Autowired
    private CampusinfoCountryService campusinfoCountryService;

    @ModelAttribute
    public CampusinfoAbroad get(@RequestParam(required = false) String id) {
        CampusinfoAbroad entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = campusinfoAbroadService.get(id);
        }
        if (entity == null) {
            entity = new CampusinfoAbroad();
        }
        return entity;
    }

    @RequiresPermissions("campusinfoeducation:campusinfoAbroad:view")
    @RequestMapping(value = {"list", ""})
    public String list(CampusinfoAbroad campusinfoAbroad, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<CampusinfoAbroad> page = campusinfoAbroadService.findPage(new Page<CampusinfoAbroad>(request, response), campusinfoAbroad);
        List<CampusinfoAbroad> campusinfoAbroads = campusinfoAbroadService.findList(campusinfoAbroad);
        model.addAttribute("campusinfoAbroads", campusinfoAbroads);
        model.addAttribute("page", page);
        return "modules/campusinfoeducation/campusinfoAbroadList";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoAbroad:view")
    @RequestMapping(value = "form")
    public String form(CampusinfoAbroad campusinfoAbroad, Model model) {
        // 地区信息
        CampusinfoRegion campusinfoRegion = new CampusinfoRegion();
        List<CampusinfoRegion> campusinfoRegions = campusinfoRegionService.findList(campusinfoRegion);
        // 国家信息
        CampusinfoCountry campusinfoCountry = new CampusinfoCountry();
        List<CampusinfoCountry> campusinfoCountries = campusinfoCountryService.findList(campusinfoCountry);
        model.addAttribute("campusinfoCountries", campusinfoCountries);
        model.addAttribute("campusinfoRegions", campusinfoRegions);
        model.addAttribute("campusinfoAbroad", campusinfoAbroad);
        return "modules/campusinfoeducation/campusinfoAbroadForm";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoAbroad:edit")
    @RequestMapping(value = "save")
    public String save(CampusinfoAbroad campusinfoAbroad, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, campusinfoAbroad)) {
            return form(campusinfoAbroad, model);
        }
        campusinfoAbroadService.save(campusinfoAbroad);
        addMessage(redirectAttributes, "保存出国信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoAbroad/?repage";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoAbroad:edit")
    @RequestMapping(value = "delete")
    public String delete(CampusinfoAbroad campusinfoAbroad, RedirectAttributes redirectAttributes) {
        campusinfoAbroadService.delete(campusinfoAbroad);
        addMessage(redirectAttributes, "删除出国信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoAbroad/?repage";
    }
    @RequiresPermissions("campusinfoeducation:campusinfoAbroad:edit")
    @RequestMapping(value = "status")
    public String status(CampusinfoAbroad campusinfoAbroad, RedirectAttributes redirectAttributes) {
        campusinfoAbroad.setStatus(CampusinfoOrderStat.DONE.getValue());
        campusinfoAbroadService.status(campusinfoAbroad);
        CampusinfoPay campusinfoPay = new CampusinfoPay();
        campusinfoPay.setId(campusinfoAbroad.getOrderCode());
        campusinfoPay.setStat(CampusinfoOrderStat.DONE.getValue());
        campusinfoPayService.status(campusinfoPay);
        addMessage(redirectAttributes, "修改订单状态");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoAbroad/?repage";
    }
}